package Gouride.LiGuiping;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CalldieActivity extends Activity {
	
	class TeleListener extends PhoneStateListener {
		  private AutoRedialService manager;
		     public TeleListener(AutoRedialService a)
		     {
		       this.manager = a;
		     }
		  @Override
		  public void onCallStateChanged(int state, String incomingNumber) {
		   super.onCallStateChanged(state, incomingNumber);
		   switch (state) {
		   // 当处于待机状态中
		   case TelephonyManager.CALL_STATE_IDLE: {
		    manager.Log("IDLE");
		    //android.util.Log.v("TeleListener", "IDLE"); 
		    if (manager.ShouldStop() || manager.LastCallSucceed()) {         
		     manager.stopSelf();  
		     break;
		    }            
		    PhoneCall();    
		    break;
		   }
		    // 当处于正在拨号出去，或者正在通话中
		   case TelephonyManager.CALL_STATE_OFFHOOK: {
		    manager.Log("OFFHOOK");
		    //android.util.Log.v("TeleListener", "OFFHOOK"); 
		    mJustCall = true;
		    //Timer t = new Timer();  
		    break;
		   }
		    // 外面拨进来，好没有接拨号状态中..
		   case TelephonyManager.CALL_STATE_RINGING: {
		    manager.Log("RINGING");
		    //android.util.Log.v("TeleListener", "RINGING"); 
		    break;
		   }
		   default:
		    break;
		   }
		  }
		  
	int mDialedCount;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        PhoneCall();
    }
    
    private void PhoneCall()
    {
     try {
      Thread.sleep(2000);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } 
     boolean mJustCall = false;
        Uri localUri = Uri.parse("tel:" + "10086");
        Intent call = new Intent(Intent.ACTION_CALL, localUri);
        call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);;
        startActivity(call);
        //android.util.Log.v("TeleListener", "start to call"); 
        
		mDialedCount ++;
    }
}